package VerticalPrototypes;

import java.sql.*;

/**
 * @author Tyler
 */

//Class for adding an item to an existing user account's inventory.
public class AddItemToInventory {
    
    //Instance of this class being used.
    private static AddItemToInventory instance = null;
    
    public AddItemToInventory()
    {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //Hardcoded database location.
        String url = "snyamathi.dyndns.org:37965";
        String user = "root";
        String password = "baconatmidnight";
        
        //Hardcoded prototype user_id and item_id and status code.
        int user_id = 1;    //For a specific user.
        int item_id = 1;    //Adding a specific item to their inventory.
        int status = 101;   //If a user is adding an item to their inventory, it's assumed they currently own it.

        try {
            //Connect to the databse.
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();

            //Add passed item_id to user_id's inventory.
            //See if the item is already in the inventory first.
				//Check to see if it's in the global database first?
            if(!(rs == (st.executeQuery("SELECT * FROM userinventory WHERE itemID = '" + item_id +"'"))))
            {
				//INSERT INTO userinventory (itemID, userID, status, ownerID) VALUES (1, 1, 101, 1);
                rs = st.executeQuery("INSERT INTO userinventory (itemID, userID, status, ownerID) VALUES (" + item_id + ", " + user_id + ", " + status + ", " + user_id + ")");
            }
            
            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
                //Something happens.
        } finally {
            //Close connections safely.
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                    //Something happens.
            }
        }
    }
    
    //Execute the addition of item to inventory from user side.
    public static AddItemToInventory user_AddItemToInventory()
    {
        if(instance == null)
            instance = new AddItemToInventory();
        return instance;
    }  
}